﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Practices.Unity;
using Microsoft.Practices.ServiceLocation;
using SkeletonNET.Base.Crud.CrudManager;
using Moq;

namespace SkeletonNET.Data.Tests
{
    [TestClass]
    public class CrudManagerTests
    {
        [TestInitialize]
        public void Initialize()
        {
            var container = new UnityContainer();
            ServiceLocator.SetLocatorProvider(() => new UnityServiceLocator(container));
            ServiceLocator.Current.GetInstance<IUnityContainer>().RegisterType(typeof(ICrudManager<>), typeof(CrudManager<>));
        }

        [TestMethod]
        public void DoesCrudManagerInject()
        {
            var manager = ServiceLocator.Current.GetInstance<ICrudManager<TestEntity>>();
            Assert.IsNotNull(manager);
        }

        [TestMethod]
        public void TestCanForceCrudManagerImplementation()
        {
            var managerMock = new Mock<CrudManager<TestEntity>>();
            var entity = new TestEntity() { ID = 200};

            ServiceLocator.Current.GetInstance<IUnityContainer>()
                .RegisterInstance<ICrudManager<TestEntity>>(managerMock.Object);
            var manager = ServiceLocator.Current.GetInstance<ICrudManager<TestEntity>>();
            manager.Insert(entity);
            manager.Delete(entity);
            manager.Update(entity);
            manager.DeleteByID(entity);

            managerMock.Verify(x => x.Delete(entity));
            managerMock.Verify(x => x.Insert(entity));
            managerMock.Verify(x => x.Update(entity));
            managerMock.Verify(x => x.DeleteByID(entity));
        }
    }
}
